package com.cast.health.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cast.health.mapper.CheckGroupMapper;
import com.cast.health.pojo.CheckGroup;
import com.cast.health.pojo.CheckItem;
import com.cast.health.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper, CheckGroup> implements CheckGroupService {

    @Autowired
    private CheckGroupMapper mapper;

    /**
     * 根据检查组的id，查询检查项的ids数据
     *
     * @param id
     * @return
     */
    @Override
    public List<CheckItem> selectByCheckItemId(Integer id) {
        //调用mapper方法查询
        List<CheckItem> list = mapper.selectByCheckItemIds(id);
        //判断是否查到
        if (list.size() == 0) {
            return null;
        }
        //查到返回数据
        return list;
    }


    /**
     * 传入每一个检查项id 和得到一个对应的检查组,建立中间表
     *
     * @param id
     * @param idItem
     */
    @Override
    public boolean addGroupAndItem(Integer id, Integer[] idItem) {
        //调用mapper写sql语句添加中间表关联信息
        for (int i = 0; i < idItem.length; i++) {

            int j = mapper.addItemAndGroup(id, idItem[i]);
            //判断逻辑是否添加成功
            if (j <= 0) {
                return false;
            }
        }
        return true;
    }


}
